package com.wmeimob.fastboot.jiubei.controller;

import com.wmeimob.fastboot.jiubei.dto.ShopCartDTO;
import com.wmeimob.fastboot.jiubei.entity.Member;
import com.wmeimob.fastboot.jiubei.entity.ShopCart;
import com.wmeimob.fastboot.jiubei.result.RestResponse;
import com.wmeimob.fastboot.jiubei.result.RestResults;
import com.wmeimob.fastboot.starter.security.context.SecurityContext;
import com.wmeimob.fastboot.jiubei.service.ShopCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author wjt
 * @version 1.0
 * @className ShopCartController
 * @description 【购物车】控制器
 * @date Thu Aug 20 10:06:35 CST 2020
 **/
@RestController
@RequestMapping("shopCart")
@Slf4j
public class ShopCartController {

    @Resource
    private ShopCartService shopCartService;

    @GetMapping
    public RestResults queryShopCartList(ShopCart shopCart) {
        fillUserId(shopCart);
        List<ShopCartDTO> shopCarts = shopCartService.queryShopCartList(shopCart);
        return RestResponse.makeOKRsp(shopCarts);
    }

    @GetMapping("count")
    public RestResults queryShopCartCount(ShopCart shopCart) {
        fillUserId(shopCart);
        return RestResponse.makeOKRsp(shopCartService.queryCount(shopCart));
    }

    @PostMapping("/add")
    public RestResults addShopCart(@RequestBody ShopCart shopCart) {
        fillUserId(shopCart);
        shopCartService.addShopCart(shopCart);
        return RestResponse.makeOKRsp();
    }

    @PostMapping("/delete")
    public RestResults deleteShopCart(@RequestBody ShopCart shopCart) {
        fillUserId(shopCart);
        shopCartService.deleteShopCart(shopCart);
        return RestResponse.makeOKRsp();
    }

    private void fillUserId(ShopCart shopCart) {
        Member member = SecurityContext.getUser();
        shopCart.setUserId(member.getId());
    }
}
